package com.wceManage.manage.controller;


import com.wceManage.beans.JCours;
import com.wceManage.beans.SysUser;
import com.wceManage.beans.page.PageList;
import com.wceManage.manage.util.UserUtils;
import com.wceManage.services.CourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author jiangwei
 * @since 20/2/15
 */
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired(required = false)
    private CourseService courseService;

    /**
     * 查询列表
     *
     * @param cours
     * @return
     */
    @RequiresPermissions("sys:course:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(JCours cours, HttpServletRequest request)
            throws InstantiationException, IllegalAccessException {

        request.setAttribute("cours", cours);

        return "course/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param cours
     * @return
     */
    @RequiresPermissions("sys:course:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, JCours cours) {
        SysUser user = UserUtils.getUser();

        if (user != null) {
            cours.setSystemId(courseService.findSystemId(user.getAccount()));
        }
        return courseService.queryPage(pageList, cours);
    }

    /**
     * 新增修改
     *
     * @param cours
     * @return
     */
    @RequiresPermissions("sys:course:edit")
    @RequestMapping(value = {"add", "edit"}, method = RequestMethod.GET)
    public String edit(JCours cours, Model model) {

        if (cours.getId() == null) { // add
            cours = new JCours();
        } else {
            HashMap<String, Object> map = new HashMap();
            map.put("courId", cours.getId());
            cours = courseService.queryById(map);
        }

        model.addAttribute("cours", cours);

        return "course/edit";
    }

    @RequiresPermissions("sys:course:edit")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(JCours cours) {

        SysUser user = UserUtils.getUser();

        if (user != null) {
            cours.setSystemId(courseService.findSystemId(user.getAccount()));

            cours.setCreateUser(user.getUserId().toString());
            cours.setUpdateUser(user.getUserId().toString());
            cours.setStatus(0);

            courseService.save(cours);
        }
        return "redirect:list";
    }

    @RequiresPermissions("sys:course:edit")
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public String detail(Integer courId, Model model) {

        SysUser user = UserUtils.getUser();

        HashMap<String, Object> map = new HashMap();
        map.put("courId", courId);
        map.put("systemId", courseService.findSystemId(user.getAccount()));

        JCours cours = courseService.queryById(map);

        model.addAttribute("cours", cours);

        return "course/detail";
    }

    @RequiresPermissions("sys:course:delete")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public Object delete(Integer id, Model model) {

        SysUser user = UserUtils.getUser();

        HashMap<String, Object> map = new HashMap();
        map.put("courId", id);
        map.put("systemId", courseService.findSystemId(user.getAccount()));

        courseService.deleteByPrimaryKey(map);

        String result = "删除成功";

        return result;
    }
}
